package uit.minito.views;

import uit.minito.timduongxebuyt.StaticVariables;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;

public class CircleButton extends Button{
	private boolean isAtFullAlpha;
	private float minAlpha=0.6f;
	private float maxAlpha=1;
	private void initialize(Context context){
		isAtFullAlpha=true;
		doAnimation(1, minAlpha, 0);
		
	}
	
	public void fadeIn(){ 
		if(isAtFullAlpha==false){
			doAnimation(minAlpha, maxAlpha, StaticVariables.SLIDE_DURATION);
		}
	}
	
	
	
	
	public void fadeOut(){
		if(isAtFullAlpha==true){
			doAnimation(maxAlpha, minAlpha, StaticVariables.SLIDE_DURATION);
		}
	}
	
	public void autoFade(){
		if(isAtFullAlpha){
			fadeOut();
		}else{  
			fadeIn();   
		}
	}

	public CircleButton(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public CircleButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public CircleButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);    
	}

	
	public void doAnimation(float _from,float _to, long _duration){  
		AlphaAnimation alphaAnim=new AlphaAnimation(_from, _to);
		alphaAnim.setFillAfter(true);
		alphaAnim.setDuration(_duration);//(StaticVariables.SLIDE_DURATION);
		alphaAnim.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}  
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
			
				isAtFullAlpha=!isAtFullAlpha;
				
				
			}
		});
		this.startAnimation(alphaAnim);
	}
}
